package com.powerunion.datacollection.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.log4j.Logger;
import com.powerunion.datacollection.config.ConfigUtil;
import com.powerunion.datacollection.dao.SmsDao;
import com.powerunion.util.DBUtils;
import com.powerunion.util.log.LogHelper;

public class SmsDaoImpl extends JdbcBase implements SmsDao {
	// 初始化日志类
	private static final Logger logger = LogHelper.getInstance().getLogger(
			SmsDaoImpl.class);

	/**
	 * 直接保存提示到短信表
	 * 
	 * @param msg
	 * @param userCode
	 * @throws Exception
	 */
	public void saveSMS(Connection conn, String msg, String userCode)
			throws Exception {
		PreparedStatement ps = null;
		try {
			String dbwizardName = ConfigUtil.getInstance().getParameter(
					"system.dbwizard.name") == null ? "dbwizard" : ConfigUtil
					.getInstance().getParameter("system.dbwizard.name");
			String userMobile = this.getUserMobile(conn, userCode);

			ps = conn
					.prepareStatement("insert into "
							+ dbwizardName
							+ ".sms_tbl(sms_seq,send_user,mobl_nmbr,sms_msg,send_stat,sms_type,save_time) values(?,?,?,?,0,0,?)");
			ps.setLong(1, genID());
			ps.setString(2, userCode);
			ps.setString(3, userMobile);
			ps.setString(4, msg);
			ps.setTimestamp(5, new java.sql.Timestamp(System
					.currentTimeMillis()));
			ps.executeUpdate();
		} catch (Exception e) {
			logger.error("保存信息到短信表失败！", e);
			throw e;
		} finally {
			DBUtils.close(ps);
		}
	}

	public void saveSMS(String msg, String userCode) throws Exception {
		PreparedStatement ps = null;
		Connection conn = null;
		
		try {
			conn = this.getConnection();
			String dbwizardName = ConfigUtil.getInstance().getParameter(
					"system.dbwizard.name") == null ? "dbwizard" : ConfigUtil
					.getInstance().getParameter("system.dbwizard.name");
			String userMobile = this.getUserMobile(conn, userCode);

			ps = conn
					.prepareStatement("insert into "
							+ dbwizardName
							+ ".sms_tbl(sms_seq,send_user,mobl_nmbr,sms_msg,send_stat,sms_type,save_time) values(?,?,?,?,0,0,?)");
			ps.setLong(1, genID());
			ps.setString(2, userCode);
			ps.setString(3, userMobile);
			ps.setString(4, msg);
			ps.setTimestamp(5, new java.sql.Timestamp(System
					.currentTimeMillis()));
			ps.executeUpdate();
		} catch (Exception e) {
			logger.error("保存信息到短信表失败！", e);
			throw e;
		} finally {
			DBUtils.close(ps, conn);
		}
	}

	private long genID() {
		long id = System.currentTimeMillis();
		int raCode = (new java.util.Random()).nextInt(99999);
		while (raCode < 10000) {
			raCode = raCode * 10;
		}
		id = id * 100000 + raCode;
		return id;
	}

	private String getUserMobile(Connection conn, String userCode)
			throws Exception {
		String mobile = null;
		PreparedStatement ps = null;
		ResultSet rsData = null;
		try {
			ps = conn.prepareStatement(this.sqlGetUserMobile());
			ps.setString(1, userCode);
			rsData = ps.executeQuery();
			if (rsData.next()) {
				mobile = rsData.getString(1);
			}
		} catch (Exception e) {
			logger.error("得到用户手机号失败！", e);
		} finally {
			DBUtils.close(rsData, ps);
		}
		return mobile;
	}

	protected String sqlGetUserMobile() {
		return "select distinct mobi_tel from base_user where user_code=?";
	}

}
